package com.viking.order.service.impl;

import com.viking.order.api.StockService;
import com.viking.order.entity.Order;
import com.viking.order.mapper.OrderMapper;
import com.viking.order.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class OrderServiceImpl implements OrderService {


    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private StockService stockService;

    /**
     * 下单
     * @GlobalTransactional :seata分布式事务注解
     */
    @GlobalTransactional
    @Override
    public Order create(Order order) {
        // 插入能否成功
        orderMapper.insert(order);
        // 扣减库存
        stockService.reduct( order.getProductId());
        System.out.println("调用完成");

        // 异常 会进行全局事务回滚
        int a=1/0;

        return order;
    }


    /**
     * skywalking链路追踪测试， @Trace标记，要记录传参及返回+注解 @Tags
     */
    @Override
    @Trace
    @Tag(key="getAll",value="returnedObj")
    public List<Order> getAll() throws InterruptedException {
        // 结合 skywalking面板中的性能刨析 测试
        TimeUnit.SECONDS.sleep(2);
        return orderMapper.selectAll();
    }

    /**
     *  skywalking链路追踪测试, @Trace标记，  要记录传参及返回+注解 @Tags
     *  key = 方法名 ； value = returnedObj 返回值 arg[0] 参数
     */
    @Override
    @Trace
    @Tags({@Tag(key="getById",value="returnedObj"),
            @Tag(key="getById",value="arg[0]")})
    public Order getById(Integer id) {
        return orderMapper.selectByPrimaryKey(id);
    }
}
